// ListFontDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ListFont.h"
#include "ListFontDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// ListFontDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ListFont.h"
#include "ListFontDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CListFontDlg dialog

CListFontDlg::CListFontDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CListFontDlg::IDD, pParent)
{
    //{{AFX_DATA_INIT(CListFontDlg)
        // NOTE: the ClassWizard will add member initialization here
    //}}AFX_DATA_INIT
    // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CListFontDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CListFontDlg)
    DDX_Control(pDX, IDC_SFTTREE1, m_Tree);
    //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CListFontDlg, CDialog)
    //{{AFX_MSG_MAP(CListFontDlg)
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CListFontDlg message handlers

void CListFontDlg::AddFont(LPCTSTR lpszFontName, LPLOGFONT lplf)
{
    _bstr_t FontName(lpszFontName);

    if (m_vTree->Items->Count > 0 && m_vTree->Items->FindCellText(0, 0, FontName) >= 0)
        return; // already added

    FONTDESC fd;
    memset(&fd, 0, sizeof(fd));
    fd.lpstrName = FontName;
    fd.cySize.int64 = 10 * 10000L; // 10pt font
    fd.sWeight = FW_NORMAL;
    IFontDispPtr pFontDisp;
    HRESULT hr = OleCreateFontIndirect(&fd, IID_IFontDisp, (void**)&pFontDisp);
    if (FAILED(hr)) return;

    long ItemIndex = m_vTree->Items->Add(FontName);
    m_vTree->Item[ItemIndex]->Level = 1;
    m_vTree->Cell[ItemIndex][1]->Text = FontName;
    m_vTree->Cell[ItemIndex][0]->PutRefFont(pFontDisp);

}

/*static*/ int CALLBACK CListFontDlg::FontEnumeratorProc(ENUMLOGFONTEX *lpelfe, NEWTEXTMETRICEX *lpntme, DWORD FontType, LPARAM lParam)
{
    CListFontDlg* PTHIS = (CListFontDlg*) lParam;

    PTHIS->AddFont((LPCTSTR)lpelfe->elfFullName, &lpelfe->elfLogFont);
    ++PTHIS->m_FontCounter;
    if (PTHIS->m_FontCounter >= MAXFONTS)
        return 0; // done
    else
        return 1; // continue
}


BOOL CListFontDlg::OnInitDialog()
{
    CDialog::OnInitDialog();

    // Set the icon for this dialog.  The framework does this automatically
    //  when the application's main window is not a dialog
    SetIcon(m_hIcon, TRUE);			// Set big icon
    SetIcon(m_hIcon, FALSE);		// Set small icon

    m_vTree	= m_Tree.GetControlUnknown();
    ASSERT(m_vTree != NULL);

    m_FontCounter = 0;

    long ItemIndex = m_vTree->Items->Add(_T("Screen Fonts"));


    CDC* pDC = GetDC();
    LOGFONT lf;
    memset(&lf, 0, sizeof(lf));
    lf.lfCharSet = DEFAULT_CHARSET;
    EnumFontFamiliesEx(pDC->m_hDC, &lf, (FONTENUMPROC) FontEnumeratorProc, (LPARAM) this, 0);

    return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CListFontDlg::OnPaint() 
{
    if (IsIconic())
    {
        CPaintDC dc(this); // device context for painting

        SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

        // Center icon in client rectangle
        int cxIcon = GetSystemMetrics(SM_CXICON);
        int cyIcon = GetSystemMetrics(SM_CYICON);
        CRect rect;
        GetClientRect(&rect);
        int x = (rect.Width() - cxIcon + 1) / 2;
        int y = (rect.Height() - cyIcon + 1) / 2;

        // Draw the icon
        dc.DrawIcon(x, y, m_hIcon);
    }
    else
    {
        CDialog::OnPaint();
    }
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CListFontDlg::OnQueryDragIcon()
{
    return (HCURSOR) m_hIcon;
}